<?php
namespace app\admin\controller\office;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use think\Request;
/**
 * @NodeAnnotation(
 *     title="工具箱",
 *     parent="admin/office.index/index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1,
 * )
 */
class Chapter extends Admin
{
    use ControllerCurd;
    public $model = '';
    public $doc_id;

    public function initialize()
    {
        parent::initialize();
        $this->doc_id = $this->request->param("doc_id",0,'intval');
        $this->assign([
            "docId" => $this->doc_id
        ]);
        $this->model = new \app\admin\model\office\ChapterModel();
    }
    /**
     * @NodeAnnotation(
     *     title="文档章节",
     *     parent="admin/office.document",
     *     is_menu=0,
     *     is_view=1,
     *     auth=1,
     *     MenuRoot="admin/office.index"
     * )
     */
    public function  index(Request $request)
    {
        $keywords  = $request->param("keywords");

        $where = [];

        if($keywords) array_push($where,['post_title',"like","%$keywords%"]);

        $postList = $this->model->where($where)
            ->order("update_time","desc")
            ->where("doc_id",$this->doc_id)
            ->paginate();

        $this->assign([
            "postList"=> $postList,
            "search"  => [
                "keywords" => $keywords
            ]
        ]);
        return view();
    }

    /**
     * @NodeAnnotation(
     *     title="添加章节",
     *     parent="admin/office.chapter",
     *     MenuRoot="admin/office.index",
     *     is_menu = 0
     * )
     */
    public function addpost(Request $request)
    {
        $this->add(url("admin/office.chapter/index",["doc_id"=>$this->doc_id]));
        return view();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑章节",
     *     MenuRoot="admin/office.chapter",
     *     parent="admin/office.document",
     *     is_menu = 0
     * )
     */
    public function editpost(Request $request,$id=0)
    {
        $post = $this->model->with('user')->find($id);

        $input = $request->post();
        if(empty($input['post_status'])) $input['post_status'] = 0;
        if(empty($input['recommended'])) $input['recommended'] = 0;
        if(empty($input['is_top'])) $input['is_top'] = 0;

        $this->edit(url("admin/office.chapter/index",["doc_id"=>$this->doc_id]),function(){},$input);

        $this->assign([
            "post"         => $post,
        ]);
        return view();
    }

    /**
     * @NodeAnnotation(
     *     title="删除章节",
     *     parent="admin/office.chapter",
     *     MenuRoot="admin/office.index",
     *     is_menu = 0
     * )
     */
    public function delpost()
    {
        $this->del();
    }
}